package com.open.party.cloud.view.home.jiCengDangJian.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.ZyzHuoDongEntity;
import com.sinothk.android.utils.XUtils;

import java.util.ArrayList;
import java.util.List;

import com.sinothk.android.utils.inters.OnSuperListener;

public class ZyzHuoDongRequestListAdapter extends RecyclerView.Adapter<ZyzHuoDongRequestListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<ZyzHuoDongEntity.RequestDTOListBean> mData;
    private Context mContext;
//    private OnSuperListener<ZyzHuoDongEntity.RequestDTOListBean> onItemClickListener;

    private ZyzHuoDongRequestListAdapter instance;

    public ZyzHuoDongRequestListAdapter(Context mContext) {
        instance = this;
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

//    public void setOnSuperListener(OnSuperListener<ZyzHuoDongEntity.RequestDTOListBean> onItemClickListener) {
//        this.onItemClickListener = onItemClickListener;
//    }

    public void setData(List<ZyzHuoDongEntity.RequestDTOListBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public ArrayList<ZyzHuoDongEntity.RequestDTOListBean> getData() {
        return mData;
    }

    public void updateData(List<ZyzHuoDongEntity.RequestDTOListBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ZyzHuoDongRequestListAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.zyz_huo_dong_request_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ZyzHuoDongRequestListAdapter.ViewHolder holder, final int position) {
        // 绑定数据
        final ZyzHuoDongEntity.RequestDTOListBean itemData = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemData.getName()));
        holder.tipTv.setText(itemData.getRecNum() + "/" + itemData.getQuantum());

        if (mData.get(position).isSelected()) {
            holder.radioBtn.setChecked(true);
        }else{
            holder.radioBtn.setChecked(false);
        }

        holder.rootView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean isSelected = mData.get(position).isSelected();

                for (int i = 0; i < mData.size(); i++) {
                    // 全部设为未选中
                    mData.get(i).setSelected(false);
                }
                // 设置当前选中、未选中状态
                mData.get(position).setSelected(!isSelected);
                notifyDataSetChanged();
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        RelativeLayout rootView;
        TextView titleTv, tipTv;
        RadioButton radioBtn;

        ViewHolder(View itemView) {
            super(itemView);
            rootView = itemView.findViewById(R.id.rootView);

            titleTv = itemView.findViewById(R.id.titleTv);
            tipTv = itemView.findViewById(R.id.tipTv);

            radioBtn = itemView.findViewById(R.id.radioBtn);
        }
    }
}